using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.Caching;
using System.Collections;

namespace DataProvider
{
    public static class DataLayerCache
    {
         private const String lockStr = "";

        private static Cache cache;

        private static int defaultDuration = 5600;

        static DataLayerCache()
        {
            cache = HttpRuntime.Cache;
            if (Config.Items.ContainsKey("CacheDuration"))
            {
                defaultDuration = int.Parse(Config.Items["CacheDuration"]);
            }
            if (defaultDuration <= 0)
            {
                defaultDuration = 5600;
            }
        }

        public static object GetObjFromCache  (String key)
        {
            return cache[key];
        }

        public static void PutObjIntoCache(String key, Object value)
        {
           
            PutObjIntoCache(key, value, null, defaultDuration);
        }

        public static void PutObjIntoCache(String key, List<String> dependencyFiles,Object value)
        {
            PutObjIntoCache(key, value, dependencyFiles, defaultDuration);
        }

        public static void PutObjIntoCache(String key, Object value,int duration)
        {
            PutObjIntoCache(key, value, null, duration);
        }

        public static void PutObjIntoCache(String key, Object value, List<String> dependencyFiles, int dur)
        {
            if (value == null || key ==null)
                return;

            CacheDependency dependency = null;
            if (dependencyFiles != null && dependencyFiles.Count > 0)
            {
                dependency = new CacheDependency(dependencyFiles.ToArray());
            }
            lock (lockStr)
            {
                if (cache.Get(key) != null)
                    cache.Remove(key);

                cache.Add(key,
                          value,
                          dependency,
                          DateTime.Now.AddMinutes(dur),
                          TimeSpan.Zero,
                          CacheItemPriority.High,
                          null);
            }
        }

        public static void Clean()
        {
            if(cache == null)
                return;
            lock (lockStr)
            {
                List<String> keySet = new List<string>();
                IDictionaryEnumerator iterator = cache.GetEnumerator();
                while (iterator.MoveNext())
                {
                    keySet.Add(iterator.Key.ToString());
                }
                foreach (String key in keySet)
                {
                    cache.Remove(key);
                }
            }
        }
        public static void Remove(String key)
        {
            lock (lockStr)
            {
                cache.Remove(key);
            }
        }
    }
    }

